package cn.tedu.travelsystem.hotel.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.base.response.StatusCode;
import cn.tedu.travelsystem.base.utils.AccessLimitUtil;
import cn.tedu.travelsystem.hotel.mapper.HotelMapper;
import cn.tedu.travelsystem.hotel.pojo.dto.HotelDTO;
import cn.tedu.travelsystem.hotel.pojo.entity.Hotel;
import cn.tedu.travelsystem.hotel.pojo.vo.HotelVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hotels")
@Api(tags = "12.酒店模块")
public class HotelController {

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private AccessLimitUtil accessLimitUtil; // 引入访问限制工具类

    // 添加酒店信息
    @PostMapping
    @ApiOperation("添加酒店信息")
    @ApiOperationSupport(order = 10)
    public JsonResult addHotel(@Validated @RequestBody HotelDTO dto, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("HotelDTO = " + dto);
        Hotel hotel = new Hotel();
        BeanUtils.copyProperties(dto, hotel);
        int rows = hotelMapper.insert(hotel);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 删除酒店信息
    @DeleteMapping("/{hotelId}")
    @ApiOperation("删除酒店信息")
    @ApiOperationSupport(order = 20)
    public JsonResult deleteHotel(@PathVariable Integer hotelId, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("hotelId = " + hotelId);
        int rows = hotelMapper.deleteById(hotelId);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 更新酒店信息
    @PutMapping("/{hotelId}")
    @ApiOperation("更新酒店信息")
    @ApiOperationSupport(order = 30)
    public JsonResult updateHotel(@PathVariable Integer hotelId, @Validated @RequestBody HotelDTO dto, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("hotelId = " + hotelId + ", HotelDTO = " + dto);
        Hotel hotel = new Hotel();
        BeanUtils.copyProperties(dto, hotel);
        hotel.setHotel_id(hotelId);
        int rows = hotelMapper.update(hotel);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 查询单个酒店信息
    @GetMapping("/{hotelId}")
    @ApiOperation("查询单个酒店信息")
    @ApiOperationSupport(order = 40)
    public JsonResult getHotel(@PathVariable Integer hotelId, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("hotelId = " + hotelId);
        Hotel hotel = hotelMapper.selectById(hotelId);
        if (hotel != null) {
            HotelVO vo = new HotelVO();
            BeanUtils.copyProperties(hotel, vo);
            return JsonResult.ok(vo);
        } else {
            return new JsonResult(StatusCode.DATA_NOT_FOUND);
        }
    }

    // 查询所有酒店信息
    @GetMapping
    @ApiOperation("查询所有酒店信息")
    @ApiOperationSupport(order = 50)
    public JsonResult getAllHotels(HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("查询所有酒店信息");
        List<Hotel> hotels = hotelMapper.selectAll();
        List<HotelVO> vos = new ArrayList<>();
        for (Hotel hotel : hotels) {
            HotelVO vo = new HotelVO();
            BeanUtils.copyProperties(hotel, vo);
            vos.add(vo);
        }
        return JsonResult.ok(vos);
    }
}
